import employess from '@/assets/constant/employees.js'
import store from '@/store/index.js'
export default {
  // 自定义指令
  // 默认图片
  imgerror: {
    inserted(dom, obj, vnode) {
      dom.src = dom.src || obj.value
      dom.onerror = function() {
        dom.src = obj.value
      }
    },
    componentUpdated(dom, obj) {
      dom.src = dom.src || obj.value
    },
    onbind(dom) {
      dom.onerror = null
    }
  },
  // 聘用形式转换
  hireType: {
    inserted(dom, obj, vnode) {
      if (obj) {
        const res = employess.hireType.find((item) => item.id === obj.value)
        dom.innerText = res.value || '-'
      } else {
        dom.innerText = '-'
      }
    },
    componentUpdated(dom, obj) {
      if (obj) {
        const res = employess.hireType.find((item) => item.id === obj.value)
        dom.innerText = res.value || '-'
      } else {
        dom.innerText = '-'
      }
    }
  },
  // 树形组件失焦的处理
  clickOutSide: {
    inserted(dom, obj, vnode) {
      document.addEventListener('click', (e) => {
        if (!dom.contains(e.target)) {
          const callback = obj.value
          callback()
        }
      })
    },
    onbind() {
      document.removeEventListener('click')
    }
  },
  control: {
    inserted(dom, obj, vnode) {
      if (!store.state.user.userInfo.roles.points.includes(obj.value)) {
        dom.style.display = 'none'
      }
    },
    componentUpdated(dom, obj) {
      if (!store.state.user.userInfo.roles.points.includes(obj.value)) {
        dom.style.display = 'none'
      }
    }
  }
}
